from time import sleep

import scrapy
from lxml import etree

from ..items import global_stock_indices


class GlobalStocksSpider(scrapy.Spider):
    name = "global_stocks"
    allowed_domains = ["quote.eastmoney.com"]
    start_urls = ["https://quote.eastmoney.com/center/gridlist.html#global_asia"]

    def parse(self, response):

        item = global_stock_indices()
        html=etree.HTML(response.text)
        table= html.xpath("//table[@id='table_wrapper-table']/tbody/tr")
        for tr in table:

            arr = tr.xpath('.//td')

            # 国家名称
            item['country_name'] = arr[1].xpath('.//a/text()')
            # 最新价
            item['latest_price'] = arr[2].xpath('.//span/text()')
            # 涨跌额
            # rising_falling_amounts = arr[3]
            item['rising_falling_amounts'] = arr[3].xpath('.//span/text()')
            # 涨跌幅
            # percentage_change = arr[4]
            item['percentage_change'] = arr[4].xpath('.//span/text()')
            # 开盘价
            # opening_price = arr[5]
            item['opening_price'] = arr[5].xpath('.//span/text()')
            # 最高价
            # maximum_price = arr[6]
            item['maximum_price'] = arr[6].xpath('.//span/text()')
            # 最低价
            # bottom_price = arr[7]
            item['bottom_price'] = arr[7].xpath('.//span/text()')
            # 昨收价
            # closing_price = arr[8]
            item['closing_price'] = arr[8].xpath('./text()')
            # 振幅
            # amplitude = arr[9]
            item['amplitude'] = arr[9].xpath('./text()')

            # print(item['country_name'])

            yield item


